package com.we823.project.controller.manage;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.we823.project.controller.AbstractSimpleBaseController;
import com.we823.project.core.web.WebConstants;
import com.we823.project.model.StationModel;
import com.we823.project.repository.service.StationService;
import com.we823.utils.web.RequestUtil;

@Controller
@RequestMapping(WebConstants.MANAGE_STATION)
public class StationController extends AbstractSimpleBaseController<StationModel, Integer> {

	private String baseUrl = WebConstants.MANAGE_STATION_TEMPLATE;
	
	private StationService stationService;
	
	@Autowired
	public StationController(StationService stationService) {
		super(stationService);
		this.stationService = stationService;
		super.baseUrl = baseUrl;
		setResourceIdentity("system:station");
	}

	@Override
	public Page<StationModel> getData(PageRequest pageRequest, HttpServletRequest request, Model model, boolean pageable) {
		Page<StationModel> stationPage = null;
		if(pageable){
			if(pageRequest!=null){
				stationPage = stationService.findAll(pageRequest);
			}
		}else{
			stationPage = new PageImpl<>(stationService.findAll());
		}
		return stationPage;
	}

	@Override
	public StationModel getEntity(HttpServletRequest request, Integer id,String oper) {
		StationModel stationModel = new StationModel();
		String name = RequestUtil.getValueByParam(request, "name");
		String code = RequestUtil.getValueByParam(request, "code");
		String status = RequestUtil.getValueByParam(request, "status");
		Date addtime = RequestUtil.getValueByParam(request, "addtime",new Date());
		
		stationModel.setName(name);
		stationModel.setCode(code);
		stationModel.setStatus(status);
		stationModel.setAddtime(addtime);
		
		if(id!=null && id>0){
			stationModel.setId(id);
			stationModel.setEdittime(new Date());
		}
		return stationModel;
	}

	@Override
	protected Sort getSort(HttpServletRequest request) {
		return new Sort(Direction.DESC,"addtime");
	}
}
